<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple">
          <p></p>
          <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-light">
          <p>备件需求计划表</p>
          <FormPanel ref="formPanel1" :list="formList1" :cols="2" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle1" />
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <p></p>
          <FormPanel ref="formPanel2" :list="formList2" :cols="1" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle2" />
        </div>
      </el-col>
      <el-col :span="12">
        <p class="elspan"></p>

        <span>*勾选参数后，备件信息下发后将更新所有OEM备件库存的销售价</span>
      </el-col>
    </el-row>

    <el-row :gutter="10">
      <el-col :span="14">
        <p>备件单据打印单位信息</p>
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel3" :list="formList3" :cols="1" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle3" />
        </div>
      </el-col>

      <el-col :span="10">
        <el-col :span="24">
          <p>发料设置</p>
          <div class="grid-content bg-purple">
            <FormPanel ref="formPanel4" :list="formList4" :cols="1" :formType="type" :labelWidth="150" :isSubmitBtn="false" @formChange="formChangeHandle4" />
          </div>
        </el-col>
        <el-col :span="24">
          <p>仓库</p>
          <div class="grid-content bg-purple">
            <FormPanel ref="formPanel5" :list="formList5" :cols="1" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle5" />
          </div>
        </el-col>
        <el-col :span="24">
          <p>参数</p>
          <div class="grid-content bg-purple">
            <FormPanel ref="formPanel6" :list="formList6" :cols="1" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle6" />
          </div>
        </el-col>
      </el-col>
    </el-row>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findAllRepair, updateBasicParam } from '@/api/repair/basicParameters';

export default {
  name: 'PartModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;
    this.saveData1 = null;
    this.saveData2 = null;
    this.saveData3 = null;
    this.saveData4 = null;
    this.saveData5 = null;
    this.saveData6 = null;
    return {
      formList: this.createFormList(),
      formList1: this.createFormList1(),
      formList2: this.createFormList2(),
      formList3: this.createFormList3(),
      formList4: this.createFormList4(),
      formList5: this.createFormList5(),
      formList6: this.createFormList6()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  mounted() {
    this.queryDefaultPa();
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.partNo;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '工单中保险公司不为空备件价格类型取',
          fieldName: '1',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择工单中保险公司不为空备件价格类型取', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '内部领用价格类型取',
          fieldName: 'parts_internalCollarPrice',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择内部领用价格类型取', trigger: 'change' }]
        }
      ];
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '采购周期(天)',
          fieldName: '2',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入采购周期(天)', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '到货周期(天)',
          fieldName: '3',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入到货周期(天)', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '每月备件工作天数(天)',
          fieldName: '4',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入每月备件工作天数(天)', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '月权值系数',
          fieldName: '5',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入月权值系数', trigger: 'blur' }]
        }
      ];
    },
    createFormList2() {
      return [
        {
          type: 'SELECT',
          label: '货运单和采购入库单是否为1对多',
          fieldName: '6',
          placeholder: '请选择',
          style: { width: '300px' },
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择货运单和采购入库单是否为1对多', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '建议销售价更新销售价',
          fieldName: '7',
          placeholder: '请选择',
          style: { width: '300px' },
          itemList: this.createDictList('1004'),

          rules: [{ required: false, message: '请选择建议销售价更新销售价', trigger: 'change' }]
        }
      ];
    },
    createFormList3() {
      return [
        {
          type: 'INPUT',
          label: '名称',
          fieldName: 'Print_name',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '电话',
          fieldName: 'Print_phone',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入电话', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'Print_address',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入地址', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '传真',
          fieldName: 'Print_fax',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择传真', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'Print_zipCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择邮编', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '开户行',
          fieldName: 'Print_bankAccount',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择开户行', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '银行账号',
          fieldName: '8',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择银行账号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '税号',
          fieldName: 'Print_taxCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择税号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '声明',
          fieldName: 'Print_statement',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请选择声明', trigger: 'blur' }]
        }
      ];
    },
    createFormList4() {
      return [
        {
          type: 'SELECT',
          label: '发料时显示成本价',
          fieldName: 'parts_costPrice',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择发料时显示成本价', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '发料单打印备件库存',
          fieldName: '8',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择发料单打印备件库存', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '发料时必须制定领料',
          fieldName: '9',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择发料时必须制定领料', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否自动打印发料单',
          fieldName: '10',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否自动打印发料单', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '发料打印备件显示顺序',
          fieldName: '11',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择发料打印备件显示顺序', trigger: 'change' }]
        }
      ];
    },
    createFormList5() {
      return [
        {
          type: 'SELECT',
          label: '仓库关单',
          fieldName: '12',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择仓库关单', trigger: 'change' }]
        }
      ];
    },
    createFormList6() {
      return [
        {
          type: 'INPUT',
          label: '备件税率',
          fieldName: '13',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入备件税率', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle1(val) {
      this.saveData1 = val;
    },
    formChangeHandle2(val) {
      this.saveData2 = val;
    },
    formChangeHandle3(val) {
      this.saveData3 = val;
    },
    formChangeHandle4(val) {
      this.saveData4 = val;
    },
    formChangeHandle5(val) {
      this.saveData5 = val;
    },
    formChangeHandle6(val) {
      this.saveData6 = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      this.$refs.formPanel1.SUBMIT_FORM();
      this.$refs.formPanel2.SUBMIT_FORM();
      this.$refs.formPanel3.SUBMIT_FORM();
      this.$refs.formPanel4.SUBMIT_FORM();
      this.$refs.formPanel5.SUBMIT_FORM();
      this.$refs.formPanel6.SUBMIT_FORM();
      let obj = Object.assign({}, this.saveData, this.saveData1, this.saveData2, this.saveData3, this.saveData4, this.saveData5);
      let res = await updateBasicParam(obj);
      if (res.resultCode === 200) {
        this.test();
      }
    },
    async queryDefaultPa() {
      let res = await findAllRepair();
      // 表1
      // 0 工单中保险公司不为空备件价格类型取
      this.formList[1].initialValue = res.data.parts_internalCollarPrice;

      // 表2
      // 0
      // 1
      // 2
      // 3

      // 表3
      // 0
      // 1

      // 表4
      this.formList3[0].initialValue = res.data.Print_name;
      this.formList3[1].initialValue = res.data.Print_phone;
      this.formList3[2].initialValue = res.data.Print_address;
      this.formList3[3].initialValue = res.data.Print_fax;
      this.formList3[4].initialValue = res.data.Print_zipCode;
      this.formList3[5].initialValue = res.data.Print_bankAccount;
      // this.formList3[6].initialValue = res.data.Print_name; 银行账号
      this.formList3[7].initialValue = res.data.Print_taxCode;
      this.formList3[8].initialValue = res.data.Print_statement;

      // 表5
      this.formList4[0].initialValue = res.data.parts_costPrice;
      // 1
      // 2
      // 3
      // 4

      // 表6
      // 表7
    },

    closeDialog(param) {
      this.$refs.formPanel.RESET_FORM();
      this.$refs.formPanel1.RESET_FORM();
      this.$refs.formPanel2.RESET_FORM();
      this.$refs.formPanel3.RESET_FORM();
      this.$refs.formPanel4.RESET_FORM();
      this.$refs.formPanel5.RESET_FORM();
      this.queryDefaultPa();
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
    }
  }
};
</script>

<style lang="less" scoped>
p {
  background-color: #d3dce6;
  height: 20px;
  line-height: 20px;
  margin-bottom: 10px;
}
.elspan {
  margin-bottom: 70px;
}
span {
  top: 50px;
  color: red;
}

.el-row {
  margin-bottom: 40px;
}
</style>
